﻿using Dx.DDD.Domain;
using Dx.DDD.Repositories;
using Dx.Test.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Dx.Test.MySqlEntityFrameworkCore
{
    public static class ModelBuilderExtension
    {
        public static void ConfiguredTable(this ModelBuilder builder) 
        {
            builder.Entity<AccountInfo>(b => {
                b.ToTable(TableConsts.DbTablePrefix+"AccountInfo");
                b.Property(x=>x.UserName).HasMaxLength(30).IsRequired();
                b.Property(x=>x.Password).HasMaxLength(30).IsRequired();
                //b.Property(x=>x.ConcurrencyStamp).HasMaxLength(40);
                b.Property(x=>x.Email).HasMaxLength(80);
                b.HasIndex(x=>x.Id);
                //并行配置
                //b.ConfigureConcurrencyStamp();
            });
        }

        public static void Configured(this IServiceCollection services,IConfiguration configuration) 
        {
            var connStr = configuration["ConnectionStrings:MyDbcontext"];
            services.AddScoped<IAccountInfoRepository,AccountInfoRepository>();
            //Net8以后支持多个DbContext注入
            //services.AddScoped<IUnitOfWork,UnitOfWork<MyDbContext>>();
            services.AddDbContext<DbContext, MyDbContext>(opt => {
                opt.UseMySql(connStr, new MySqlServerVersion(new Version(5, 7, 36)));
            },enableWork:true,1024);
        }
    }
}
